<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/11/6 0006
 * Time: 下午 12:06
 */

namespace app\controller;


use app\Request;
use app\validate\User;
use think\exception\ValidateException;
use think\facade\Validate;
use think\validate\ValidateRule;

class Verify
{

    function index(){

        try
        {
            //print_r(request()->all());
           // validate(User::class)->check(request()->all());
            validate(User::class)->batch(true)->scene('edit')->check([
                "name"=>"",
                "price"=>334,
                "email"=>"35235qq.com"
            ]);
        }catch (ValidateException $e){
            dump($e->getError());
        }
    }


    function route($id){

        return "id=".$id;

    }
    function rule(){

//        $validate = Validate::rule(
//            [
//                'name|用户名'=>"require|max:20|checkName:cs",
//                "price"=>"number|between:1,100",
//                "email"=>"email"
//            ]
//        );
        $validate = Validate::rule(
            [
                //'name|用户名'=>ValidateRule::isRequire(null,"你写你的名字啊，吊毛")->max(20),
                'name|用户名'=>ValidateRule::isRequire()->max(20,"太长了"),
                "price"=>ValidateRule::isNumber()->between([1,100]),
                "email"=>ValidateRule::isEmail(null,"邮箱不对"),
                //'test'=>'require'
                //'test'=>ValidateRule::isRequire()
                //'test'=>'must'
                //'test'=>ValidateRule::isNumber()
                //'test'=>ValidateRule::isChs()
                //'test'=>'length:5'
                //'test'=>ValidateRule::length(6,"长度不够啊")
                //'test'=>'eq:2'
                //'test'=>ValidateRule::eq(24,"填写的test不对")
                //'test'=>'confirm:price'
                //'test'=>ValidateRule::confirm("price","test和price要一样知道不")
                'age'=>'unique:users' //验证users表age字段的值是否唯一
            ]
        );

//        $validate = Validate::rule([
//            "name"=>function($value){
//
//            }
//        ]);
//        $validate->message([
//            "name.require"=>"吊毛，写名字啊"
//        ]);
        $result = $validate->batch(true)->check(
            [
                "name"=>"sdfds",
                "price"=>33,
                "email"=>"35235@qq.com",
                "test"=>"dg",
                "age"=>100
            ]
        );


        if (!$result){

            dump( $validate->getError());
        }



    }

    function single(){


        //dump(Validate::isEmail("234234@qq.com"));
        //dump(Validate::activeUrl("http://www.baidu.com"));
        //dump(Validate::isNumber("http://www.baidu.com"));
        dump(Validate::checkRule("40","number|between:20,100"));

    }

    function token(){


       // echo session("__token__");
        //echo "<br/>";
        //echo input("__token__");

//        if(!request()->checkToken()){
//
//            return "令牌不对";
//        }

        $validate = Validate::rule([
            "name"=>"require|token"
        ]);
        $validate->message([
            "name.require"=>"姓名要写啊",
            "name.token"=>"令牌不对啊，兄弟"
        ]);

        $ret = $validate->batch(true)->check(request()->all());

        if(!$ret){
            return json($validate->getError());
        }
    }
}